package com.dotc.kotlinmvvmdemo.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.dotc.kotlinmvvmdemo.utils.ActivityCollector
import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel

abstract class BaseActivity<T: ViewDataBinding,V: ViewModel> : AppCompatActivity(),CoroutineScope by MainScope(){
    var binding:T?=null
    var viewModel:V?=null

    /**
     * 设置viewModel
     */
    abstract fun modelClass():Class<V>?

    /**
     * 设置布局文件
     */
    abstract fun layout():Int

    /**
     * 数据初始化
     */
    abstract fun initData()

    /**
     * LiveData监听
     */
    abstract fun initObserve()

    var immersionBar: ImmersionBar? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ActivityCollector.addActivity(this)
        activityTopView()
        if(modelClass()!=null)
        {
            binding= DataBindingUtil.setContentView<T>(this,layout())
            viewModel=ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(modelClass()!!)
        }else{
            setContentView(layout())
        }
        initData()
        initObserve()
    }


    private fun activityTopView() {
        try {
          /*  immersionBar = ImmersionBar.with(this)
            if (this is SplashActivity) {
                immersionBar?.hideBar(BarHide.FLAG_HIDE_STATUS_BAR)?.init()

          } else if (this is MainActivity ) {
              immersionBar?.fitsSystemWindows(false)
                          ?.statusBarDarkFont(false, 0.5f)
                          ?.navigationBarColor(R.color.color_FFFFFF) //.hideBar(BarHide.FLAG_SHOW_BAR)
                          ?.init()
            } else {
                immersionBar?.statusBarColor(R.color.color_FFFFFF)
                    ?.fitsSystemWindows(true)
                    ?.statusBarDarkFont(true, 0.5f)
                    ?.navigationBarColor(R.color.color_FFFFFF)
                    ?.init()
            }*/
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this)
        cancel()
    }


}